package com.ipzoe.web.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ipzoe.cms.domain.Agreement;
import com.ipzoe.cms.domain.Article;
import com.ipzoe.cms.service.IAgreementService;
import com.ipzoe.cms.service.IArticleService;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.oms.domain.Order;
import com.ipzoe.oms.service.IOrderService;
import com.ipzoe.system.service.ISysDictDataService;
import com.ipzoe.web.convert.AgreementConvert;
import com.ipzoe.web.convert.OrderConvert;
import com.ipzoe.web.model.OrderDetailDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/order")
@Slf4j
public class OrderController extends WebBaseController {

    @Autowired
    private IOrderService iOrderService;
    @Autowired
    private IArticleService iArticleService;
    @Autowired
    private IAgreementService iAgreementService;
    @Autowired
    private ISysDictDataService iSysDictDataService;

    /**
     * 查询订单详情
     */
    @GetMapping("/info/{id}")
    public String detail(@PathVariable Long id, Model model, HttpServletRequest request)
    {
        Order order = iOrderService.getById(id);
        OrderDetailDTO orderDetailDTO = OrderConvert.INSTANCE.convert(order);
        if(ObjectUtil.isNotNull(orderDetailDTO)){
            orderDetailDTO.setMemFeeArticle(iArticleService.getById(3));
            orderDetailDTO.setMemInterestsArticle(iArticleService.getById(4));

            //支付方式
            if(ObjectUtil.isNotNull(orderDetailDTO.getPayType())){
                orderDetailDTO.setPayTypeStr(iSysDictDataService.selectDictLabel("oms_pay_type",String.valueOf(orderDetailDTO.getPayType())));
            }

            //协议列表
            LambdaQueryWrapper<Agreement> agreementLqw = new QueryWrapper<Agreement>().lambda()
                    .eq(Agreement::getPackageId, order.getPackageId()).orderByAsc(Agreement::getId);
            List<Agreement> agreementList = iAgreementService.list(agreementLqw);
            orderDetailDTO.setAgreementList(AgreementConvert.INSTANCE.convert(agreementList));

            model.addAttribute("order", orderDetailDTO);
        }

        return getLocaleHtml("order.html", request);
    }
}
